package com.taptech.doufu.ui.view;

import android.content.Context;
import android.graphics.Color;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.event.EventBusMyTag;
import com.taptech.doufu.bean.TagModel;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.util.ui.HiddenAnimUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class TagSelectView extends LinearLayout {



    public interface OnSelectTag{

        void onChange(int type, String tag);

        void onChangeCustomTag(ArrayList<String> list);

        void jumpToCustomTag(ArrayList<String> list);
    }


    private TagModel.TagBean bean;
    private boolean hasChild;
    private Context context;

    private ImageView ivArrow,ivChildArrow,ivIcon,ivChildIcon;


    private TagViewGroup rvgParent,rvgChild;
    private TextView tvParentTitle,tvChildTitle;
    private LinearLayout llContentParent,llChildContentParent,llChild;
    private RelativeLayout rlTitle,rlChildTitle;

    private int height,childHeight = 0;
    private ArrayList<String> tagsList = new ArrayList<>();
    private OnSelectTag onSelectTag;



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMyTagChange(EventBusMyTag event){
        if (event != null && !TextUtils.isEmpty(event.getTag()) &&
                event.getTag().equals(EventBusMyTag.tag)&&
                event.getList()!=null&&
                bean.isCustom()) {  //如果是自定义标签
            tagsList = event.getList();
            onSelectTag.onChangeCustomTag(tagsList);
            updateMyTags();
        }
    }

    public TagSelectView(Context context) {
        super(context);
    }


    public TagSelectView(Context context, TagModel.TagBean bean,OnSelectTag onSelectTag,
                         String selectTag,String selectChildType,ArrayList<String> customTags){
        super(context);
        this.onSelectTag = onSelectTag;
        EventBus.getDefault().register(this);
        this.context = context;
        if (bean == null) {
            return;
        }
        this.bean = bean;
        if (!TextUtils.isEmpty(this.bean.getcTitle())){
            hasChild = true;
        }else{
            hasChild = false;
        }
        init(selectTag,selectChildType);
        //自定义标签初始化
        if (bean.isCustom() && customTags!=null && customTags.size()>0) {
            this.tagsList = customTags;
            updateMyTags();
        }
    }



    public TagSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TagSelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void init(String selectTag,String selectChildTag) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_tag_select, null);
        rvgParent = (TagViewGroup) view.findViewById(R.id.rvgParent);
        llContentParent = (LinearLayout) view.findViewById(R.id.llContentParent);
        ivArrow = (ImageView) view.findViewById(R.id.ivArrow);
        ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
        tvParentTitle = (TextView) view.findViewById(R.id.tvParentTitle);
        tvParentTitle.setText(bean.getTitle());
        GlideUtil.displayImage(ivIcon,bean.getLeftIcon());
        GlideUtil.displayImage(ivArrow,bean.getRightIcon());
        rlTitle = (RelativeLayout) view.findViewById(R.id.rlTitle);
        rlTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.isCustom()) {
                    onSelectTag.jumpToCustomTag(tagsList);
                }else {
                    if (height == 0) {
                        height = llContentParent.getHeight();
                    }
                    HiddenAnimUtils.newInstance(context,llContentParent,ivArrow,height).toggle();
                }
            }
        });

        llChild = (LinearLayout) view.findViewById(R.id.llChild);
        rvgChild = (TagViewGroup) view.findViewById(R.id.rvgChild);
        llChildContentParent = (LinearLayout) view.findViewById(R.id.llChildContentParent);
        ivChildArrow = (ImageView) view.findViewById(R.id.ivChildArrow);
        ivChildIcon = (ImageView) view.findViewById(R.id.ivChildIcon);
        tvChildTitle = (TextView) view.findViewById(R.id.tvChildTitle);
        rlChildTitle = (RelativeLayout) view.findViewById(R.id.rlChildTitle);
        rlChildTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rvgChild.getChildCount() > 0) {
                    if (childHeight == 0) {
                        childHeight = llChildContentParent.getHeight();
                    }
                    HiddenAnimUtils.newInstance(context,llChildContentParent,ivChildArrow,childHeight).toggle();
                }
            }
        });
        if (hasChild) {
            llChild.setVisibility(VISIBLE);
            tvChildTitle.setText(bean.getcTitle());
            GlideUtil.displayImage(ivChildIcon,bean.getcLeftIcon());
            GlideUtil.displayImage(ivChildArrow,bean.getcRightIcon());
        }else{
            llChild.setVisibility(GONE);
        }

        setParentContent(selectTag,selectChildTag);
        addView(view);
    }

    /**
     * 设置父布局标签
     */
    private void setParentContent(String selectTag,String selectChildTag) {
        if (bean.getItems() == null) {
            return;
        }
        //设置父布局内容
        for (int i = 0; i < bean.getItems().size(); i++) {
            final TagModel.TagItem item = bean.getItems().get(i);
            tagsList.add(item.getName());
            final TextView text = getTagItem(item.getName());
            if (item.getName().equals(selectTag)) {
                setTextBg(text,true);
                setTextColor(text,true);
                //设置子布局
                if (hasChild) {
                    setChild(item.getChilds(),selectChildTag);
                }
            }
            text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bean.isCustom()) {
                        //跳转到自定义页面
                        onSelectTag.jumpToCustomTag(tagsList);
                    }else{
                        for (int i = 0; i < rvgParent.getChildCount(); i++) {
                            if (rvgParent.getChildAt(i) instanceof TextView) {
                                setTextColor((TextView) rvgParent.getChildAt(i),false);
                            }
                            setTextBg(rvgParent.getChildAt(i),false);
                        }
                        setTextBg(text,true);
                        setTextColor(text,true);
                        onSelectTag.onChange(bean.getType(),item.getName());
                        //设置子布局
                        if (hasChild) {
                            setChild(item.getChilds(),"");
                        }
                    }

                }
            });
            rvgParent.addView(text);
        }
    }

    /**
     * 更新自定义标签
     */
    private void updateMyTags() {

        rvgParent.removeAllViews();
        //设置父布局内容
        for (int i = 0; i < tagsList.size(); i++) {
            final String name = tagsList.get(i);
            final TextView text = getTagItem(name);
            text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bean.isCustom()) {
                        //跳转到自定义页面
                        onSelectTag.jumpToCustomTag(tagsList);
                    }else{
                        for (int i = 0; i < rvgParent.getChildCount(); i++) {
                            if (rvgParent.getChildAt(i) instanceof TextView) {
                                setTextColor((TextView) rvgParent.getChildAt(i),false);
                            }
                            setTextBg(rvgParent.getChildAt(i),false);
                        }
                        setTextBg(text,true);
                        setTextColor(text,true);
                        onSelectTag.onChange(bean.getType(),name);
                    }

                }
            });
            rvgParent.addView(text);
        }
    }

    /**
     * 获取单个标签
     * @param name
     * @return
     */
    private TextView getTagItem(String name) {
        final TextView text = new TextView(context);
        text.setClickable(true);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        text.setLayoutParams(params);
        text.setTextSize(15);
        text.setMinWidth((ScreenUtil.getScreenWidth(context)-ScreenUtil2.dip2px(38+40+26))/3);
        text.setText(name);
        text.setGravity(Gravity.CENTER);
        setTextBg(text,false);
        setTextColor(text,false);
        text.setPadding(ScreenUtil2.dip2px(12), ScreenUtil2.dip2px(10), ScreenUtil2.dip2px(12), ScreenUtil2.dip2px(10));
        return text;
    }


    /**
     * 填充子标签布局
     * @param list
     */
    private void setChild(List<TagModel.TagChild> list,String selectChildTag) {
        if (list==null)
            return;
        onSelectTag.onChange(bean.getcType(),selectChildTag);
        rvgChild.removeAllViews();
        //设置子布局内容
        for (int i = 0; i < list.size(); i++) {
            final TagModel.TagChild item = list.get(i);
            final TextView text = getTagItem(item.getName());
            if (item.getName().equals(selectChildTag)) {
                setTextBg(text,true);
                setTextColor(text,true);
            }
            text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bean.isCCustom()) {

                    }else{
                        for (int i = 0; i < rvgChild.getChildCount(); i++) {
                            if (rvgChild.getChildAt(i) instanceof TextView) {
                                setTextColor((TextView) rvgChild.getChildAt(i),false);
                            }
                            setTextBg(rvgChild.getChildAt(i),false);
                        }
                        setTextBg(text,true);
                        setTextColor(text,true);
                        onSelectTag.onChange(bean.getcType(),item.getName());
                    }

                }
            });
            rvgChild.addView(text);
        }
    }


    private void setTextColor(TextView text,boolean isSelect) {
        if (isSelect) {
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                text.setTextColor(getResources().getColor(R.color.theme_main_dark));
            }else{
                text.setTextColor(Color.parseColor("#fe6e6e"));
            }
        }else{
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                text.setTextColor(getResources().getColor(R.color.title_text_dark));
            }else{
                text.setTextColor(Color.parseColor("#303030"));
            }
        }
    }

    private void setTextBg(View view,boolean isSelect){
        if (isSelect) {
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                view.setBackgroundResource(R.drawable.biankuang_tag_select_night);
            }else{
                view.setBackgroundResource(R.drawable.biankuang_tag_select);
            }
        }else{
            if(WeMediaApplication.getInstance().isDayNightMode_Night){

                view.setBackgroundResource(R.drawable.biankuang_tag_unselect_night);
            }else{

                view.setBackgroundResource(R.drawable.biankuang_tag_unselect);
            }
        }
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }
}
